package com.zhuozhou.jdyc.main.main.adapter;

import android.content.Context;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.ClassifyItemBinding;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/8/18
 * Describe:分类
 */
public class ClassifyAdapter extends BaseRecyclerViewAdapter<TabItemBean, ClassifyItemBinding> {

    public OnTabItemClickListener mOnTabItemClickListener;

    public ClassifyAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.classify_item;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);
        baseViewHolder.getBinding().getRoot().setOnClickListener(v -> {
            List<TabItemBean> list = new ArrayList<>();
            for (TabItemBean item : mList) {
                if (item.getIsView()) {
                    item.setIsView(false);
                }
                list.add(item);
            }
            list.get(pos).setIsView(true);
            setList(list);
            notifyDataSetChanged();
            if (mOnTabItemClickListener != null) {
                mOnTabItemClickListener.onTabItemClick(baseViewHolder.getBinding().getRoot(), getRealPosition(baseViewHolder));
            }
        });
    }

    @Override
    public void onBindHolder(BaseViewHolder<ClassifyItemBinding> holder, int pos) {
        holder.getBinding().tvTop.setText(mList.get(pos).getTopName());
        if (mList.get(pos).getIsView()) {
            holder.getBinding().llClassify.setBackgroundColor(mContext.getResources().getColor(R.color.bg_color));
            holder.getBinding().vLine.setVisibility(View.VISIBLE);
        } else {
            holder.getBinding().llClassify.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            holder.getBinding().vLine.setVisibility(View.GONE);
        }
    }

    public void setOnTabItemClickListener(OnTabItemClickListener l) {
        mOnTabItemClickListener = l;
    }

    public interface OnTabItemClickListener {
        void onTabItemClick(View view, int position);
    }

}
